//
//  ViewController.m
//  AnchorPoint
//
//  Created by 吴高烽 on 2016/11/19.
//  Copyright © 2016年 吴高烽. All rights reserved.
//
/**
 1. 先设置锚点再设置frame，因为设置锚点时frame会被重新计算
 2. 触摸事件没有执行，需要调用transitionContext 的 complete 方法
 
 */
#import "ViewController.h"
#import "YKAnimationPresentedProxy.h"
#import "YKAnimationDismissedProxy.h"
@interface ViewController ()
/** 蓝色View */
@property (nonatomic ,weak)UIView *blueView;
@end

@implementation ViewController
//控制器的View是通过调用 loadView 方法来创建的
/*
 作用：loadView方法是用来创建UIViewController的View。首先判断下有没有指定的storyboard或者Xib，如果指定，就会加载它们描述的控制器的View，如果没有指定，创建一个空的View。
 什么时候调用：每次访问UIViewController的View，当View为nil，就会调用loadView方法。
*/

//-(void)loadView{
//    [super loadView];
//    CGFloat offsetY = 2.0;
//    self.view = [[UIView alloc]init];
//    self.view.backgroundColor = [UIColor redColor];
//    self.view.layer.anchorPoint = CGPointMake(0.5, offsetY);
//    self.view.frame = [UIScreen mainScreen].bounds ;
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(didRotation:)];
//    [self.view addGestureRecognizer:pan];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    CGFloat offsetY = 2.0;
//    CGFloat windowX = self.view.frame.size.width ;
//    CGFloat windowY = self.view.frame.size.height ;
    UIView *blueView = [[UIView alloc]init];
    blueView.backgroundColor = [UIColor blueColor];
    blueView.layer.anchorPoint = CGPointMake(0.5, offsetY);
    blueView.frame = [UIScreen mainScreen].bounds ;

//    blueView.frame = CGRectMake(0, (offsetY-0.5)*windowY, windowX, windowY);
//    设置一个控件的位置是有UIView的center属性，或者CALayer的position属性
//    设置一个控件的大小使用UIView的bounds属性
//    frame这个属性是根据控件的center、bounds、transform最终计算出来的
//    [self.view addSubview:blueView];
//    self.blueView = blueView ;
//    修改blueView的锚点,(会改变view的初始位置，基于锚点位置做变换的)
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(didRotation:)];
        [blueView addGestureRecognizer:pan];
    self.view = blueView ;
}
// 旋转的手势
-(void)didRotation:(UIPanGestureRecognizer*)recognizer{
    if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) { //!> 判断旋转的角度
        if (ABS(recognizer.view.transform.b)>0.16) {
//            if (recognizer.view.transform.b>0) {
//                recognizer.view.transform = CGAffineTransformMakeRotation(M_PI_2);
//            }else{
//                recognizer.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
//            }
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            recognizer.view.transform = CGAffineTransformIdentity ; //!> 最初状态
        }
    }else{
    CGFloat offSetX = [recognizer translationInView:recognizer.view].x;
    CGFloat percent = offSetX / self.view.frame.size.width ;
    CGFloat radians = M_PI_4 * percent ;
        recognizer.view.transform = CGAffineTransformMakeRotation(radians);
    }
    
//    CGAffineTransformMake(<#CGFloat a#>, <#CGFloat b#>, <#CGFloat c#>, <#CGFloat d#>, <#CGFloat tx#>, <#CGFloat ty#>)
//    CGAffineTransformRotate(recognizer.view.transform, radians);
}
//view呈现时的动画代理
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    YKAnimationPresentedProxy *prenset = [[YKAnimationPresentedProxy alloc]init];
    return prenset ;
}
//view消失时的动画代理
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    YKAnimationDismissedProxy *dismiss = [[YKAnimationDismissedProxy alloc]init];
    return dismiss ;
}
//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
////    弧度为正顺时针旋转
//    self.blueView.transform = CGAffineTransformRotate(self.blueView.transform, M_PI_4 *0.2);
//}
//- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
//    
//}
//
//- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
